package com.sunyard.manage.util;

import com.sunyard.dal.dto.FileDTO;
//import com.sunyard.utils.unionpayutil.LogUtil;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

public class FileUtils {
    /**
     * 下载文件时，针对不同浏览器，进行附件名的编码
     *
     * @param filename 下载文件名
     * @param agent    客户端浏览器
     * @return 编码后的下载附件名
     * @throws IOException
     */
    public static String encodeDownloadFilename(String filename, String agent)
            throws IOException {
        if (agent.contains("Firefox")) { // 火狐浏览器
            filename = "=?UTF-8?B?"
                    + new BASE64Encoder().encode(filename.getBytes("utf-8"))
                    + "?=";
            filename = filename.replaceAll("\r\n", "");
        } else { // IE及其他浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        }
        return filename;
    }

    public static String downloadFile(HttpSession session, HttpServletRequest request, HttpServletResponse response, FileDTO fileDto) throws IOException {
        ServletContext servletContext = session.getServletContext();
        ServletOutputStream outputStream = response.getOutputStream();
        String filename = new String(fileDto.getFileName().getBytes("ISO-8859-1"), "UTF-8");
        filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
        String mimeType = servletContext.getMimeType(filename);
        response.setContentType(mimeType);
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        outputStream.write(fileDto.getSteam());
        outputStream.close();
        return null;
    }

    public static String downloadFile2(HttpSession session, HttpServletRequest request, HttpServletResponse response, FileDTO fileDto) throws IOException {
        ServletContext servletContext = session.getServletContext();
        ServletOutputStream outputStream = response.getOutputStream();
        String filename = new String(fileDto.getFileName().getBytes("ISO-8859-1"), "UTF-8");
        filename =
                FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
        String mimeType = servletContext.getMimeType(filename);
        response.setContentType(mimeType);
        response.setHeader("content-disposition", "attachment;filename=" + filename);
        //输入流写入输出流
        InputStream inputStream = new ByteArrayInputStream(fileDto.getSteam());
        byte b[] = new byte[2048];
        int n;
        //循环读取 !=-1读取完毕
        while ((n = inputStream.read(b)) != -1) {
            //写入到输出流 从0读取到n
            outputStream.write(b, 0, n);
        }
        //关闭流、释放资源
        outputStream.close();
        inputStream.close();
        return null;
    }

}
